package tarantula.instrumenter;

public class SourcePos {
	public final String packageName;
	public final String fileName;
	public final int lineNum;
	public final String jimpleCode;
	
	private static final String SEP = "##";
	
	public SourcePos(String packageName, String fileName, int lineNum, String jimpleCode) {
		assert packageName != null;
		assert fileName != null;
		assert lineNum >= 0;
		assert jimpleCode != null;
		
		this.packageName = packageName;
		this.fileName = fileName;
		this.lineNum = lineNum;
		this.jimpleCode = jimpleCode;
	}
	
	@Override
	public int hashCode() {
		return 7*packageName.hashCode() + 13*fileName.hashCode() + 97*lineNum
		   + 29*jimpleCode.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof SourcePos)) {
			return false;
		}
		SourcePos sourcePos = (SourcePos)obj;
		return this.packageName.equals(sourcePos.packageName)
		       && this.fileName.equals(sourcePos.fileName)
		       && (this.lineNum == sourcePos.lineNum)
		       && this.jimpleCode.equals(sourcePos.jimpleCode);
	}
	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		//sb.append(sep);
		sb.append(packageName);
		sb.append(SEP);
		sb.append(fileName);
		sb.append(SEP);
		sb.append(lineNum);
		sb.append(SEP);
		sb.append(jimpleCode);
		//sb.append(sep);
		return sb.toString();
	}
	
	public static SourcePos parse(String str) {
		String[] strs = str.split(SEP);
		assert strs.length == 4;
		
		SourcePos sp = new SourcePos(strs[0], strs[1], Integer.parseInt(strs[2]), strs[3]);
		return sp;
	}
}
